#include "systimer.h"
#include "commonfunc.h"

SysTimer::SysTimer()
{

}

void SysTimer::f_startTimer(CnsComponentPtr com, const QVariantHash &v)
{
    CnsTimerPtr timer = com->propertyInNs("timer::timer").value<CnsTimerPtr>();
    int interval = com->propertyInNs("timer::interval").toInt();
    int elapse = com->propertyInNs("timer::elapse").toInt();
    int connectionType = com->propertyInNs("connectionType").toInt();
    connect(timer.data(), &QTimer::timeout, this, [com, v, connectionType](){
        if(v.size()==1)
            com->exec(v["method"].toString().toLatin1(), QStringList(), static_cast<Qt::ConnectionType>(connectionType));
        else
            com->exec(v["method"].toString().toLatin1(), QStringList(), static_cast<Qt::ConnectionType>(connectionType), QGenericReturnArgument(), Q_ARG(QVariantHash, v));
    });
    timer->start(elapse);
    timer->setInterval(interval);
}

void SysTimer::f_stopTimer(CnsComponentPtr com)
{
    CnsTimerPtr timer = com->propertyInNs("timer::timer").value<CnsTimerPtr>();
    timer->stop();
}
